package com.tgky.ybzb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.entity.FileInfo;
import com.tgky.ybzb.enums.MxTypeEnum;
import com.tgky.ybzb.mapper.FileInfoMapper;
import com.tgky.ybzb.utils.MPUtil;
import com.tgky.ybzb.entity.Rwghmx;
import com.tgky.ybzb.mapper.RwghmxMapper;
import com.tgky.ybzb.service.IRwghmxService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgky.ybzb.utils.TimeConvert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 任务规划模型表 服务实现类
 *
 * @author XYP
 * @since 2024-07-22
 */
@Service
public class RwghmxServiceImpl extends ServiceImpl<RwghmxMapper, Rwghmx> implements IRwghmxService {
    @Resource
    private RwghmxMapper rwghmxMapper;
    @Resource
    private FileInfoMapper fileInfoMapper;
    /**
     * 通过实体类获取QueryWrapper
     * @param pageParam 分页参数
     * @param rwghmx 实体信息
     * @return
     */
    @Override
    public QueryWrapper<Rwghmx> getQueryWrapper(PageParam pageParam, Rwghmx rwghmx){
        QueryWrapper<Rwghmx> queryWrapper = MPUtil.getQueryWrapper(pageParam);
        queryWrapper.orderByDesc("create_time");
        if(rwghmx == null){
            return queryWrapper;
        }

        //主键
        if(rwghmx.getMxid() != null){
            queryWrapper.lambda()
                    .eq(Rwghmx::getMxid, rwghmx.getMxid());
            return queryWrapper;
        }

        //TODO 此处可以根据各字段查询需求修改查询条件，eq、like、ge、gt、le、lt、ne...等等等
        queryWrapper.lambda()
                .eq(rwghmx.getMxmc() != null, Rwghmx::getMxmc, rwghmx.getMxmc())
                .eq(rwghmx.getSfmc() != null, Rwghmx::getSfmc, rwghmx.getSfmc())
                .eq(rwghmx.getJbsf() != null, Rwghmx::getJbsf, rwghmx.getJbsf())
                .eq(rwghmx.getYrwsl() != null, Rwghmx::getYrwsl, rwghmx.getYrwsl())
                .eq(rwghmx.getHs() != null, Rwghmx::getHs, rwghmx.getHs())
                .eq(rwghmx.getRwfgl() != null, Rwghmx::getRwfgl, rwghmx.getRwfgl())
                .eq(rwghmx.getYxjzh() != null, Rwghmx::getYxjzh, rwghmx.getYxjzh())
                .eq(rwghmx.getMxlx() != null, Rwghmx::getMxlx, rwghmx.getMxlx())
                .eq(rwghmx.getMxlj() != null, Rwghmx::getMxlj, rwghmx.getMxlj());
        return queryWrapper;
    }

    /**
     * 保存任务规划模型和文件
     * @param rwghmx
     * @return
     */
    @Override
    public boolean saveRwghmxAndFile(Rwghmx rwghmx) {
        int state=rwghmxMapper.insert(rwghmx);
        FileInfo fileInfo=rwghmx.getFileInfo();
        fileInfo.setType(MxTypeEnum.RWGHMX.toString());
        fileInfo.setTableId(rwghmx.getMxid());
        fileInfo.setCreateTime(TimeConvert.getCurrentDateTime());
        state=fileInfoMapper.insert(fileInfo);
        return state==1 ? true:false;
    }
}
